<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <head>
        <p:resources />
    </head>
    <body>
        <ui:composition template="./../../Template/generalTemplate.xhtml">
            <ui:define name="content">


                <h:form prependId="false" > 

                    <p:wizard showNavBar="false" showStepStatus="false"  widgetVar="wiz" flowListener="#{amendSalesOrderManagedBean.onFlowProcess}">

                        <p:tab title="Amend Sales Order">
                            <p:growl id="growl1" showDetail="true" /> 
                            <p:panel header="Amend Sales Order" style="margin-top:10px; height:430px;">  
                                <div style="height:30px;">
                                    <p:commandButton style="float:left; position:relative;" image="check" value="View Sales Quotation Detail" oncomplete="dlg3.show()"/>
                                    <p:commandButton style="margin-right:10px; position:relative;float:right;" image="check" value="View the Original Sales Order Info" oncomplete="dlg4.show()"/>
                                </div>
                                <hr/>
                                <h:panelGrid style="margin-top:3px;" columns="2" cellpadding="2">
                                    <h:outputText value="Sales Order Id: "/>
                                    <h:outputText style="font-weight:bold" value="#{amendSalesOrderManagedBean.salesOrder.id}"/>
                                    <h:outputText value="Customer:"/>
                                    <h:outputText style="font-weight:bold" value="#{amendSalesOrderManagedBean.salesOrder.customer.name}"/>
                                </h:panelGrid>    
                                <h:outputText value="LineItem List" style="margin-left:2px; color:slategray; font:12px;"/>
                                <p:dataTable  id ="lineItemList" dynamic="true" emptyMessage="No product selected" value="#{amendSalesOrderManagedBean.salesOrderLineItemList}" var="o" >

                                    <p:column>
                                        <f:facet name="header">Product Name</f:facet>
                                        #{o.product.productName}
                                    </p:column> 

                                    <p:column>
                                        <f:facet name="header">Quantity</f:facet>

                                        #{o.quantity}


                                    </p:column>

                                    <p:column id="tableUnitPrice">
                                        <f:facet name="header">Unit Price</f:facet>

                                        #{o.price}

                                    </p:column>

                                    <p:column id="tableCumAmount">
                                        <f:facet name="header">Cum Amount</f:facet>

                                        #{o.cumAmount}


                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">Edit</f:facet>
                                        <p:commandLink value =" Delete" ajax ="false" update ="lineItemList" action="#{amendSalesOrderManagedBean.deleteLineItem(o)}"/>
                                    </p:column>        
                                </p:dataTable>


                                <p:panel  style="margin-top:25px;" id="lineItemAddition" header="Add The LineItem" toggleable="true" >
                                    <table>
                                        <td width="240"><h:selectOneMenu id="productType" value="#{amendSalesOrderManagedBean.productId}">  
                                                <f:selectItems value="#{amendSalesOrderManagedBean.getProductListFromSalesQuotation()}" />
                                            </h:selectOneMenu>
                                            <p:message for="productType"/></td>

                                        <td width="240"><h:outputText value = "Quantity"/>
                                            <h:inputText id="a" value="#{amendSalesOrderManagedBean.lineItem.quantity}" />
                                            <p:message for="a"/></td>

                                        <td width="200"><h:outputText value = "Unit Price"/>
                                            <font style="color:blue;"><h:outputText id="unitPrice" value="#{amendSalesOrderManagedBean.lineItem.price}"/></font>
                                        </td>

                                        <td width="200"><h:outputText value = "Cum Amount"/>
                                            <font style="color:blue;"><h:outputText  id="cumAmount" value="#{amendSalesOrderManagedBean.lineItem.cumAmount}" /></font>
                                        </td>
                                        <p:poll interval="2"   
                                                actionListener="#{amendSalesOrderManagedBean.calculateCumAmount}" update="unitPrice cumAmount" />
                                        <td><p:commandLink value="Add LineItem" update="lineItemList growl1 lineItemAddition" ajax ="true" actionListener ="#{amendSalesOrderManagedBean.addLineItem}"/></td>
                                    </table>

                                </p:panel>








                            </p:panel>
                            <p:commandButton style="margin-top:20px; position:relative; float:left;" value ="Verify Price"  update="growl1" actionListener="#{amendSalesOrderManagedBean.verifyPrice}"/>

                            <p:commandButton style="margin-top:20px; position:relative; margin-left:400px" value="Save" actionListener="#{amendSalesOrderManagedBean.saveDraft}"/>
                            <p:commandButton style="margin-top:20px; margin-right:5px; position:relative;float:right;" value="Next" onclick="wiz.next()" ></p:commandButton>

                            <p:dialog widgetVar="dlg3" header="Sales Quotation Detail" width="1000">
                                <p:outputPanel style="margin-top:10px; margin-left:5px; width:800px;">   
                                    <h:panelGrid columns="2" cellpadding="3">
                                        <h:outputText value="Quotation ID" />  
                                        <h:panelGroup>  
                                            <h:outputText value="#{amendSalesOrderManagedBean.salesQuotation.id}" style="font-weight:bold"/>  
                                        </h:panelGroup>  

                                        <h:outputText value="Company" />  
                                        <h:panelGroup>  
                                            <h:outputText value="#{amendSalesOrderManagedBean.salesQuotation.company.name}" style="font-weight:bold"/>  
                                        </h:panelGroup> 
                                        <h:outputText value="Raise Date" /> 
                                        <h:panelGroup>  
                                            <h:outputText value="#{amendSalesOrderManagedBean.salesQuotation.raiseDate}" style="font-weight:bold">  
                                                <f:convertDateTime pattern="yyyy-MM-dd" />
                                            </h:outputText>
                                        </h:panelGroup> 

                                        <h:outputText value="Valid Until" /> 
                                        <h:panelGroup>  
                                            <h:outputText value="#{amendSalesOrderManagedBean.salesQuotation.validUntil}" style="font-weight:bold">  
                                                <f:convertDateTime pattern="yyyy-MM-dd"/>
                                            </h:outputText>
                                        </h:panelGroup>  

                                        <h:outputText value="User Discount"/><p:spacer width="10" />
                                        <h:panelGroup>  
                                            <h:outputText style="font-weight:bold" value="#{amendSalesOrderManagedBean.salesQuotation.userDiscount}"/>
                                        </h:panelGroup>  
                                    </h:panelGrid>
                                    <p:dataTable var="lineItem" value="#{amendSalesOrderManagedBean.salesQuotation.quotationLineItems}" dynamic="true" emptyMessage="No product selected">
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Proudct" />
                                            </f:facet>
                                            <h:outputText value="#{lineItem.product.productName}" />
                                        </p:column>
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Quantity (in cases)" />
                                            </f:facet>
                                            <h:outputText value="#{lineItem.quantity}" />
                                        </p:column>
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Unit Price" />
                                            </f:facet>
                                            <h:outputText value="#{lineItem.price}" />
                                        </p:column>
                                    </p:dataTable>

                                </p:outputPanel>
                            </p:dialog>
                            <p:dialog widgetVar="dlg4" header="Original Sales Order Info" width="1000">
                                <p:outputPanel style="margin-top:10px; margin-left:5px; width:800px;">   
                                    <h:panelGrid columns="2" cellpadding="3">
                                        <h:outputText value="Sales Order ID:" />  
                                        <h:panelGroup>  
                                            <h:outputText value="#{amendSalesOrderManagedBean.gettOriginalSalesOrder().id}" style="font-weight:bold"/>  
                                        </h:panelGroup>  

                                        <h:outputText value="Company: " />  
                                        <h:panelGroup>  
                                            <h:outputText value="#{amendSalesOrderManagedBean.gettOriginalSalesOrder().customer.name}" style="font-weight:bold"/>  
                                        </h:panelGroup> 
                                        <h:outputText value="Create Date: " /> 
                                        <h:panelGroup>  
                                            <h:outputText value="#{amendSalesOrderManagedBean.gettOriginalSalesOrder().dateCreated}" style="font-weight:bold">  
                                                <f:convertDateTime pattern="dd-MM-yyyy" />
                                            </h:outputText>
                                        </h:panelGroup> 

                                    </h:panelGrid>
                                    <p:dataTable var="lineItemOr" value="#{amendSalesOrderManagedBean.gettOriginalSalesOrder().salesOrderLineItems}" dynamic="true" emptyMessage="No product selected">
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Proudct" />
                                            </f:facet>
                                            <h:outputText value="#{lineItemOr.product.productName}" />
                                        </p:column>
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Quantity (in cases)" />
                                            </f:facet>
                                            <h:outputText value="#{lineItemOr.quantity}" />
                                        </p:column>
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Unit Price" />
                                            </f:facet>
                                            <h:outputText value="#{lineItemOr.price}" />
                                        </p:column>
                                    </p:dataTable>

                                </p:outputPanel>
                            </p:dialog>




                        </p:tab>


                        <p:tab title="Generate Delivery Orders" id="deliveryOder">
                            <p:growl id="growl2" showDetail="true" /> 
                            <p:panel header="Generate Delivery Orders" style= "height:450px;overflow:auto;">

                                <p style="color:blue; font:18px;">
                                    <h:selectBooleanCheckbox value="#{amendSalesOrderManagedBean.salesOrder.cashInAdvance}" />
                                    Cash In Advance
                                </p>

                                <p:dataTable  id="deliveryOrderList" dynamic="true" emptyMessage="No product selected" value="#{amendSalesOrderManagedBean.deliveryOrderList}" var="deliveryOrder" >

                                    <p:column>
                                        <f:facet name="header">Delivery Order #</f:facet>
                                        #{deliveryOrder.id}
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">Line Item</f:facet>
                                        <p:commandLink update="datatable2" ajax="true" value="Show" onclick="dlg2.show()">
                                            <f:setPropertyActionListener value="#{deliveryOrder}" target="#{amendSalesOrderManagedBean.selectedDeliveryOrder}"/>
                                        </p:commandLink>   
                                    </p:column>    

                                    <p:column>
                                        <f:facet name="header">Delivery Order Date</f:facet>
                                        <h:outputText value="#{deliveryOrder.deliveryDate}">
                                            <f:convertDateTime pattern ="yyyy-MM-dd"/>
                                        </h:outputText>   
                                    </p:column>

                                    <p:column>
                                        <f:facet name="header">Delivery Location</f:facet>
                                        <h:outputText value="#{deliveryOrder.shippingAddress.streetName},"/>
                                        <h:outputText value="#{deliveryOrder.shippingAddress.city}"/>


                                    </p:column> 

                                    <p:column>
                                        <f:facet name="header">Edit</f:facet>
                                        <p:commandLink value =" Delete" update="deliveryOrderList" ajax ="false" action="#{amendSalesOrderManagedBean.deleteDeliveryrder(deliveryOrder)}"/>
                                    </p:column> 

                                </p:dataTable>

                                <div id="subDiv" style="margin-top:30px;">
                                    <p:panel header="Create The Delivery Order" id="deliveryOrderCreation" toggleable="true" >
                                        <div id="subDiv1" style="margin-top:15px;">   
                                            <p:dataTable  id ="deOrderLineitemList" dynamic="true" emptyMessage="No product selected" value="#{amendSalesOrderManagedBean.deliveryOrderLineItemList}" var="a" >

                                                <p:column>
                                                    <f:facet name="header">Product Name</f:facet>
                                                    #{a.product.productName}
                                                </p:column> 

                                                <p:column>
                                                    <f:facet name="header">Quantity</f:facet>
                                                    #{a.quantity}
                                                </p:column>

                                                <p:column>
                                                    <f:facet name="header">Unit Price</f:facet>
                                                    #{a.price}
                                                </p:column>

                                                <p:column>
                                                    <f:facet name="header">Cum Amount</f:facet>
                                                    #{a.cumAmount}
                                                </p:column>

                                                <p:column>
                                                    <f:facet name="header">Edit</f:facet>
                                                    <p:commandLink value =" Delete" update ="deOrderLineitemList" ajax ="false" action="#{amendSalesOrderManagedBean.deleteLineItem_deliveryOrder(a)}"/>
                                                </p:column>        
                                            </p:dataTable> 
                                        </div>


                                        <div style="margin-top:15px;"> 
                                            <table> 
                                                <td width="240"><h:selectOneMenu id="productType_deliveryOrder" value="#{amendSalesOrderManagedBean.productId_dOrder}" >  
                                                        <f:selectItems value="#{amendSalesOrderManagedBean.getProductListFromSalesOrder()}" />
                                                    </h:selectOneMenu></td>
                                                <td><p:message for="productType_deliveryOrder"/></td>

                                                <td width="240"> <h:outputText value = "Quantity"/>
                                                    <h:inputText id="a2" value="#{amendSalesOrderManagedBean.lineItem.quantity}" /></td>


                                                <td width="150"> <h:outputText value = "Unit Price"/>
                                                    <font style="color:blue;"><h:outputText id="unitPrice_deliveryOrder" value="#{amendSalesOrderManagedBean.lineItem.price}"/></font></td>
                                                <td width="150"> <h:outputText value = "Amount Left"/>
                                                    <font style="color:blue;"><h:outputText  id="amountLeft_deliveryOrder" value="#{amendSalesOrderManagedBean.amountLeft}" /></font></td>

                                                <td width="150"> <h:outputText value = "Cum Amount"/>
                                                    <font style="color:blue;"><h:outputText  id="cumAmount_deliveryOrder" value="#{amendSalesOrderManagedBean.lineItem.cumAmount}" /></font></td>
                                                <td width="150"><p:commandLink value="Add LineItem" update="growl2 deliveryOrderCreation" ajax ="true" action ="#{amendSalesOrderManagedBean.addLineItemToDeliveryOrder}" />  </td>

                                                <p:poll interval="2"   
                                                        actionListener="#{amendSalesOrderManagedBean.calculateCumAmountForDeliveryOrder}" update="unitPrice_deliveryOrder cumAmount_deliveryOrder"  />
                                                <p:poll interval="2"   
                                                        actionListener="#{amendSalesOrderManagedBean.calculateAmountLeft}" update="amountLeft_deliveryOrder"  /> 


                                            </table>                                                 
                                        </div>

                                        <div style="margin-top:15px;">
                                            <table>
                                                <tr>
                                                    <td width="120"><h:outputText value = "Delivery Location :"/></td>
                                                    <td width="600"> <h:selectOneMenu id="deliveryLocation" value="#{amendSalesOrderManagedBean.deliveryLocationId}">  
                                                            <f:selectItems value="#{amendSalesOrderManagedBean.getShippingAddressList()}" />
                                                        </h:selectOneMenu>
                                                        <p:message for="deliveryLocation"/></td>
                                                    <td width="100"><h:outputText value = "Delivery Date :"/></td>
                                                    <td width="200"><p:calendar value="#{amendSalesOrderManagedBean.deliveryOrder.deliveryDate}" pattern="yyyy-MM-dd"/></td>
                                                </tr>

                                                <tr>
                                                    <td width="120"><h:outputText value ="Billing Address :"/></td>
                                                    <td width="600"><h:selectOneMenu id="billingAddress" value="#{amendSalesOrderManagedBean.billingAddressId}">  
                                                            <f:selectItems value="#{amendSalesOrderManagedBean.getBillingAddressList()}" />
                                                        </h:selectOneMenu>
                                                        <p:message for="billingAddress"/></td>
                                                </tr>
                                            </table>   

                                        </div>
                                        <p:commandButton style="margin-top:15px;" value="Create Delivery Order" update="deliveryOrderList deOrderLineitemList growl2" ajax="true" actionListener ="#{amendSalesOrderManagedBean.addDeliveryOrder}"/>
                                    </p:panel>

                                </div>

                            </p:panel>




                            <p:commandButton style ="margin-top:10px; margin-left:0px; position:relative;float:left;" value="Back" onclick="wiz.back();"></p:commandButton>
                            <p:commandButton style="margin-top:10px; position:relative; margin-left:400px" value="Save" actionListener="#{amendSalesOrderManagedBean.saveDraft}"/>
                            <p:commandButton style="margin-top:10px; margin-right:10px; position:relative; float:right;" value="Complete Amendment" update="growl2" ajax="true" actionListener ="#{amendSalesOrderManagedBean.amendSalesOrder}" ></p:commandButton>

                        </p:tab> 


                    </p:wizard> 

                </h:form>
                <h:form prependId="false">
                    <p:dialog header="DelvieryOrder LineItemList" widgetVar="dlg2" resizable="true" width="500"> 
                        <h:panelGrid id= "display">
                            <p:dataTable id ="datatable2" value="#{amendSalesOrderManagedBean.selectedDeliveryOrder.deliveryOrderLineItems}" var="lineItem">
                                <p:column>
                                    <f:facet name="header">Product</f:facet>
                                    #{lineItem.product.productName}
                                </p:column>  
                                <p:column>
                                    <f:facet name="header">Quantity</f:facet>
                                    #{lineItem.quantity}
                                </p:column>  
                                <p:column>
                                    <f:facet name="header">Unit Price</f:facet>
                                    #{lineItem.price}
                                </p:column>  
                                <p:column>
                                    <f:facet name="header">Total Price</f:facet>
                                    #{lineItem.cumAmount}
                                </p:column>  

                            </p:dataTable>  
                        </h:panelGrid>
                    </p:dialog>  

                </h:form>   
            </ui:define>

            <ui:define name="bottom">

            </ui:define>

        </ui:composition>

    </body>
</html>
